package com.wectrls.controller;

import com.wectrls.common.CommonVO;
import com.wectrls.common.utils.BindResultUtils;
import com.wectrls.entity.Category;
import com.wectrls.service.CategoryService;
import com.wectrls.service.PageService;
import com.wectrls.service.SourcesService;
import com.wectrls.vo.dto.PageData;
import com.wectrls.vo.postvo.CategoryAddVo;
import com.wectrls.vo.postvo.SourcesAddVo;
import com.wectrls.vo.queryvo.CategoryUpdateVo;
import com.wectrls.vo.queryvo.SourcesUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019-10-30 23:51
 */
@RestController
@RequestMapping(value = "/page")
public class PageController {

    @Autowired
    private SourcesService sourcesServiceImpl;

    @Autowired
    private CategoryService categoryServiceImpl;

    @Autowired
    private PageService pageServiceImpl;

    @PostMapping(value = "/sources/add")
    @RolesAllowed({"ROLE_GROUP_ADMIN"})
    public Object addNewSources(@RequestBody SourcesAddVo sourcesAddVo, BindingResult bindingResult) {
        BindResultUtils.checkBindingResult(bindingResult);
        sourcesServiceImpl.addOne(sourcesAddVo);
        PageData pageData = pageServiceImpl.getPageData();
        return new CommonVO<>(pageData);
    }

    @PostMapping(value = "/category/add")
    @RolesAllowed({"ROLE_GROUP_ADMIN"})
    public Object addNewCategory(@RequestBody CategoryAddVo categoryAddVo, BindingResult bindingResult) {
        BindResultUtils.checkBindingResult(bindingResult);
        Category category = categoryServiceImpl.addOne(categoryAddVo);
        return new CommonVO<>(category);
    }

    @GetMapping(value = "/pagedata/find")
    @RolesAllowed({"ROLE_GROUP_ADMIN", "ROLE_GROUP_USER"})
    public Object findPage() {
        return new CommonVO<>(pageServiceImpl.getPageData());
    }

    @PutMapping(value = "/category/update")
    @RolesAllowed({"ROLE_GROUP_ADMIN", "ROLE_GROUP_USER"})
    public Object updateCategory(@RequestBody CategoryUpdateVo categoryUpdateVo, BindingResult bindingResult) {
        BindResultUtils.checkBindingResult(bindingResult);
        pageServiceImpl.updateCategory(categoryUpdateVo);
        return new CommonVO<>(pageServiceImpl.getPageData());
    }

    @PutMapping(value = "/sources/update")
    @RolesAllowed({"ROLE_GROUP_ADMIN", "ROLE_GROUP_USER"})
    public Object updateSources(@RequestBody SourcesUpdateVo sourcesUpdateVo, BindingResult bindingResult) {
        BindResultUtils.checkBindingResult(bindingResult);
        pageServiceImpl.updateSources(sourcesUpdateVo);
        return new CommonVO<>(pageServiceImpl.getPageData());
    }

    @DeleteMapping(value = "/category/del/{categoryId}")
    @RolesAllowed({"ROLE_GROUP_ADMIN", "ROLE_GROUP_USER"})
    public Object delCategoryById(@PathVariable String categoryId) {
        Category category = pageServiceImpl.delCategory(categoryId);
        return new CommonVO<>(category);
    }
}
